package com.mysql

import org.apache.spark.sql.SparkSession

/**
  *
  * @author ymy.hadoop
  *
  */
object SparkToMysql {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName("SparkToMysql")
      .getOrCreate()

    // val sc = spark.sparkContext

    val df = spark.read
            .format("jdbc")
            .option("url", "jdbc:mysql://127.0.0.1/test")
            .option("dbtable", "test.girl")
            .option("user", "root")
            .option("password", "123")
            .load()

    val df2 = spark.read
       .format("jdbc")
       .option("url", "jdbc:mysql://127.0.0.1/test")
       .option("dbtable", "test.mygirl2")
       .option("user", "root")
       .option("password", "123")
       .load()

    df.createOrReplaceTempView("t")

    df2.createOrReplaceTempView("s")

    spark.sql("select t.* from t join s on t.id = s.id where t.age=100 order by s.id").show(false)

    // df.show(false)

    import spark.implicits._

    spark.stop()
  }
}
